package com.jinxiangyang.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jinxiangyang.annotation.OperateLog;
import com.jinxiangyang.domain.UserInfo;
import com.jinxiangyang.service.UserInfoService;
import com.jinxiangyang.utils.enums.BusinessType;
import com.jinxiangyang.utils.enums.OperatorType;
import com.jinxiangyang.utils.result.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/userInfo")
@Api(tags = "用户接口")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @ApiModelProperty("用户信息分页查询")
    @GetMapping("/{currentPage}/{pageSize}")
    @OperateLog(title = "用户接口", operatorType = OperatorType.ADMIN, businessType = BusinessType.OTHER)
    public AjaxResult getPageUserInfo(@PathVariable Integer currentPage,@PathVariable Integer pageSize,UserInfo userInfo){
        IPage<UserInfo> ipage = userInfoService.getPageUserInfoLike(currentPage,pageSize,userInfo);
        if (currentPage>ipage.getPages()){
            currentPage = Math.toIntExact(ipage.getPages());
            ipage = userInfoService.getPageUserInfoLike(currentPage,pageSize,userInfo);
        }
        return AjaxResult.success("查询成功",ipage);
    }

    @ApiModelProperty("用户信息添加")
    @PostMapping
    @OperateLog(title = "用户接口", operatorType = OperatorType.ADMIN, businessType = BusinessType.INSERT)
    public AjaxResult insertUserInfo(@RequestBody UserInfo userInfo){
        return userInfoService.save(userInfo)?AjaxResult.success("添加成功"):AjaxResult.error("添加失败");
    }

    @ApiModelProperty("用户信息删除")
    @DeleteMapping("/{id}")
    @OperateLog(title = "用户接口", operatorType = OperatorType.ADMIN, businessType = BusinessType.DELETE)
    public AjaxResult deleteUserInfo(@PathVariable Integer id){
        return userInfoService.removeById(id)?AjaxResult.success("删除成功"):AjaxResult.error("删除失败");
    }
    @ApiModelProperty("用户信息修改")
    @PutMapping
    @OperateLog(title = "用户接口", operatorType = OperatorType.ADMIN, businessType = BusinessType.UPDATE)
    public AjaxResult updateUserInfo(@RequestBody UserInfo userInfo) {
        return userInfoService.updateById(userInfo) ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
    }
}

